﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.Collapse
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class EntityLinkOutput:ResultBase
    {
        #region props
        private int _Entity1Count;
        /// <summary></summary>
        public int Entity1Count
        {
            get
            {
                return this._Entity1Count;
            }
            set
            {
                this._Entity1Count = value;
            }
        }

        private int _Entity2Count;
        /// <summary></summary>
        public int Entity2Count
        {
            get
            {
                return this._Entity2Count;
            }
            set
            {
                this._Entity2Count = value;
            }
        }

        private int _EntityLinked;
        /// <summary></summary>
        public int EntityLinked
        {
            get
            {
                return this._EntityLinked;
            }
            set
            {
                this._EntityLinked = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public EntityLinkOutput():base(Guid.Empty, Guid.Empty)
        {
            this._Entity1Count = 0;
            this._Entity2Count = 0;
            this._EntityLinked = 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="entity1Count"></param>
        /// <param name="entity2Count"></param>
        /// <param name="entityLinked"></param>
        public EntityLinkOutput(Guid componentID, Guid jobTicketID,
            int entity1Count, int entity2Count, int entityLinked) : base(componentID, jobTicketID)
        {
            this._Entity1Count = entity1Count;
            this._Entity2Count = entity2Count;
            this._EntityLinked = entityLinked;
        }
        #endregion

        #region Overrides of ResultBase

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Entity1Count", this.Entity1Count.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Entity2Count",this.Entity2Count.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "EntityLinked",this.EntityLinked.ToString());

            return xNode;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xNode"></param>
        /// <returns></returns>
        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                EntityLinkOutput output = new EntityLinkOutput();
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.Entity1Count =
                    int.Parse(XmlDataUtil.GetAttributeValue(xNode, "Entity1Count", "0"));
                output.Entity2Count =
                    int.Parse(XmlDataUtil.GetAttributeValue(xNode, "Entity2Count", "0"));
                output.EntityLinked =
                    int.Parse(XmlDataUtil.GetAttributeValue(xNode, "EntityLinked", "0"));

                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }

        #endregion
    }
}
